package com.bxm.springboot.test.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CacheRedisAspect {
    private static Logger LOGGER = LoggerFactory.getLogger(CacheRedisAspect.class);

    @Pointcut(value ="@annotation(com.bxm.springboot.test.aspect.CacheRedis)")
    public void pointCut(){}

    @Before(value = "pointCut() && @annotation(cacheRedis)")
    public void before(CacheRedis cacheRedis){
        LOGGER.info("The result of this method will be cached");
    }

    @AfterReturning(value = "pointCut() && @annotation(cacheRedis)",returning = "result")
    public void afterReturning(JoinPoint point, CacheRedis cacheRedis, Object result){
        String key = cacheRedis.key();
        int expireTime = cacheRedis.expireTime();
        Object[] args = point.getArgs();
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        Class<?> targetClass = point.getTarget().getClass();
        LOGGER.info("-----redis-----[key = " + key + "]"+"[expireTime = " + expireTime + "]");
        LOGGER.info("the result of this method is " + result + ",and has been cached.");
    }

}
